﻿// Astart.cpp : 定义控制台应用程序的入口点。
//
#include "Astart.h"
#include "Draw.h"
#include <iostream>
#include <vector>

using namespace std;
using namespace CPP;



vector<vector<int>> level_map = {
	{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
	{ 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1 },
	{ 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1 },
	{ 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1 },
	{ 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1 },
	{ 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1 },
	{ 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1 },
	{ 1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1 },
	{ 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1 },
	{ 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1 },
	{ 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1 },
	{ 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1 },
	{ 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1 },
	{ 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
	{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
};

//地图是否合法
bool inArea(Vec2 v2)
{
	return !(v2.y < 0 || v2.y >= level_map.size()
		|| v2.x < 0 || v2.x >= level_map[v2.y].size()
		|| level_map[v2.y][v2.x] == 1);
}

int main()
{
	Astart as(inArea);
	vector<Vec2> path;
	Vec2 start(-1, -1);
	Vec2 end(-1, -1);
	Draw d(600, 600, level_map);
	d.update();
	int x, y;
	while (true)
	{
		auto msg = GetMouseMsg();  //获取鼠标信息 (图形库)
		x = msg.x / d.size;
		y = msg.y / d.size;
		if (msg.uMsg == WM_LBUTTONDOWN)
		{
			if (start.y == -1 && d.level[y][x] == 0)  //第一次点击, 起点
			{
				start.x = x;
				start.y = y;
				d.level[start.y][start.x] = Map::visited;
				d.update();
			}
			else if (d.level[y][x] == 0)     //第二次点击 终点
			{
				end.x = x;
				end.y = y;
				d.level[end.y][end.x] = Map::end;	//设置终点的位置
				d.update();
				break;
			}
		}
	}
	bool r = as.findPath(start, end, path);
	for (auto v : path)
	{
		d.level[v.y][v.x] = Map::visited;
		d.update();
	}
	system("pause");
	return 0;
}